﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web_Oto.Business;
using Web_Oto.Common;

namespace Web_Oto.UC
{
    public partial class Products : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
                GetTopProducts();
        }

        private void GetTopProducts()
        {
            var productBus = new ProductBus();
            var showItems = ConfigurationManager.AppSettings["itemperpage"];
            var products = productBus.GetTopProducts(int.Parse(showItems));
            var sb = new StringBuilder();
            var path = ConfigurationManager.AppSettings["productPath"];
            foreach (var product in products)
            {
                sb.AppendFormat("<li><a href='/san-pham/{0}/{1}/{2}-{3}.html' class='mask_home'><div></div><img src='{4}/{5}' title='{6}' alt='{6}'></a>", 
                    CommonHelper.NiceUrl(product.CategoryParentName.ToLower()), CommonHelper.NiceUrl(product.CategoryName.ToLower()),
                    CommonHelper.NiceUrl(product.Name.ToLower()), product.Id, path, product.Image, product.TitleOfImage);
                sb.AppendFormat("<div><h5><a href='/san-pham/{0}/{1}/{2}-{3}.html' class='product-name'>{4}</a></h5></div>", CommonHelper.NiceUrl(product.CategoryParentName.ToLower()), CommonHelper.NiceUrl(product.CategoryName.ToLower()),
                    CommonHelper.NiceUrl(product.Name.ToLower()), product.Id, product.Name);
                sb.Append("</li>");
            }
            ltrTopProducts.Text = sb.ToString();
        }
    }
}